package com.lxk.service.impl;

import com.lxk.dao.AsMyMapper;
import com.lxk.dao.SeckillMapper;
import com.lxk.pojo.Seckill;
import com.lxk.pojo.SeckillExample;
import com.lxk.service.SeckillService;
import com.lxk.util.exception.GlobalException;
import com.lxk.vo.PageModel;
import com.lxk.vo.SeckillModel;
import com.lxk.vo.SeckillShowModel;
import com.lxk.vo.result.CodeMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author JohnSoybean
 * @create 2020/4/1 13:33
 * @description
 */
@Service
@Transactional
public class SeckillServiceImpl implements SeckillService {

    @Autowired
    private SeckillMapper sm;
    @Autowired
    private AsMyMapper mm;

    public Integer setSeckill(Seckill seckill) {
        int ins = sm.insertSelective(seckill);
        if(ins == 0) {
            throw new GlobalException(CodeMsg.MYSQL_CRUD_FAIL);
        }
        return seckill.getSeckillId();
    }

    public List<Seckill> getSeckillsByGoodsId(Integer goodsId) {
        SeckillExample example = new SeckillExample();
        SeckillExample.Criteria criteria = example.createCriteria();
        criteria.andGoodsIdEqualTo(goodsId);
        List<Seckill> seckillList = sm.selectByExample(example);
        return seckillList;
    }

    public void updateSeckill(Seckill seckill) {
        sm.updateByPrimaryKeySelective(seckill);
    }

    public Integer getTotalPages(Integer num) {
        SeckillExample example = new SeckillExample();
        Integer count = (int)sm.countByExample(example);
        Integer totalPages = count%num==0 ? count/num : count/num+1;
        return totalPages;
    }

    public PageModel<SeckillModel> selSeckillPage(Integer pageIndex, Integer num, Integer totalPages, Integer sort, Integer order) {
        int start = (pageIndex-1)*num;
        List<SeckillModel> list = mm.selSeckillPage(start, num, sort, order);
        PageModel<SeckillModel> model = new PageModel<SeckillModel>();
        model.setList(list);
        model.setNum(num);
        model.setPageIndex(pageIndex);
        model.setTotalPages(totalPages);
        return model;
    }

    public SeckillShowModel showSeckillGoodsInfo() {
        SeckillShowModel model = mm.selSeckillShowModel();
        if(model==null) {
            return null;
        }else {
            Date beginTime = model.getSeckillBeginTime();
            Date nowTime = new Date();
            if(beginTime.before(nowTime)) {
                model.setIsStarted(1);
            }else {
                model.setIsStarted(0);
            }
            Integer count = model.getSeckillCount();
            Date endTime = model.getSeckillEndTime();
            Integer isClosed = model.getIsClosed();
            if(count<1||endTime!=null||isClosed==1) {
                model.setIsFinished(1);
            }else {
                model.setIsFinished(0);
            }
        }
        return model;
    }

}
